<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";
import qs from "qs";

const dialogVisible = ref(false);

const dialogTitle = ref("新建用户")


const levelOptions = ref([
  {value: "10", label: "员工"},
  {value: "20", label: "经理"},
  {value: "30", label: "总监"},
  {value: "40", label: "总裁"}
])

//定义方法用来加载直属领导数据
const levelSelect = () => {
  //优化:如果已经勾选过了直属领导数据,需要再次重新选择职级的话
  //解决:需要将之前的领导数组与已选择的直属领导数据清空!
  leaderOptions.value = [];
  user.value.parentId = '';
  //拿到当前用户选择的职级
  let level = parseInt(user.value.level) + 10;
  console.log(level)
  //给后端发送请求,获取直属领导数据
  axios.get(BASE_URL + '/v1/user/select?level=' + level).then((response) => {
    if (response.data.code == 2000) {
      leaderOptions.value = response.data.data;
      console.log(leaderOptions.value)
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}

const leaderOptions = ref([])

const user = ref({
  username: "",
  phone: "",
  email: "",
  age: "",
  level: "",
  parentId: "",
  gender: "",
  status: ""
})

const user1 = ref({
  username: '',
  status: ''
})

const userArr = ref([])


//页面一加载完毕 立即执行的方法
onMounted(() => {
  select();
})

const resist1 = () => {
  user1.value = {};
  select()
}

const select = () => {
  let data = qs.stringify(user1.value)
  axios.get(BASE_URL + '/v1/user/select?' + data)
      .then(response => {
            if (response.data.code == 2000) {
              userArr.value = response.data.data
              ElMessage.success("查询成功")
            } else {
              ElMessage.error(response.data.msg)
            }
          }
      )
}

const beforeInsert = () => {
  dialogVisible.value = true;
  dialogTitle.value = "新建用户";
}


//保存用户信息
const btn = () => {
  let data = qs.stringify(user.value)
  // axios.post(BASE_URL+'/v1/user/save',user.value)
  axios.post(BASE_URL + '/v1/user/save', data)
      .then(response => {
        if (response.data.code == 2000) {
          ElMessage.success("保存成功")
          select();
        } else {
          ElMessage.error(response.data.msg)
        }
      })
  user.value = {
    username: "",
    phone: "",
    email: "",
    age: "",
    level: "",
    parentId: "",
    gender: "",
    status: ""
  }
  dialogVisible.value = false
}

const handleClose = () => {
  if (confirm('你确定要关闭弹窗吗')) {
    dialogVisible.value = false
    user.value = {};
  }
}

//删除用户
const del = (index, row) => {
  if (confirm('确定删除' + row.username + '吗？')) {
    axios.post(BASE_URL + '/v1/user/del?id=' + row.id)
        .then(response => {
          if (response.data.code == 2000) {
            select();
            ElMessage.success("删除成功")
          } else {
            ElMessage.error(response.data.msg)
          }
        })
  }
}


//更新用户
const update = (id) => {
  dialogTitle.value = "编辑用户";
  dialogVisible.value = true;
  axios.get(BASE_URL + '/v1/user/select?id=' + id)
      .then(response => {
        if (response.data.code == 2000) {
          user.value = response.data.data[0];
          let level = parseInt(user.value.level) + 10;
          //给后端发送请求,获取直属领导数据
          axios.get(BASE_URL + '/v1/user/select?level=' + level).then((response) => {
            if (response.data.code == 2000) {
              leaderOptions.value = response.data.data;
              console.log(leaderOptions.value)
            } else {
              ElMessage.error(response.data.msg);
            }
          })
          select();
        } else {
          ElMessage.error(response.data.msg)
        }
      })
}

//重置密码,RestFul风格
const resist = (userId) => {
  axios.post(BASE_URL + '/v1/user/update/password/' + userId)
      .then(response => {
        if (response.data.code == 2000) {
          ElMessage.success("重置成功")
        } else {
          ElMessage.error(response.data.msg)
        }
      })
}

const updateStatus = (id, status) => {
  axios.post(BASE_URL + '/v1/user/update/status/' + id + '/' + status)
      .then(response => {
        if (response.data.code == 2000) {
          ElMessage.success("修改成功")
        } else {
          ElMessage.error(response.data.msg)
        }
      })
}
</script>
<!--用户管理页-->
<template>
  <div style="height: 6vh;background-color: #fff;padding: 10px 20px">
    <span style="font-size: 20px;line-height: 60px">用户管理</span>
    <el-button type="primary" style="float: right;margin-top: 10px;" @click="beforeInsert">新建用户</el-button>
  </div>
  <!--  :before-close="handleClose"表示在弹窗关闭前调用自定义的处理弹窗关闭的函数-->
  <el-dialog :title="dialogTitle" v-model="dialogVisible" style="width:1000px;padding:40px" :before-close="handleClose">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="用户名">
            <el-input placeholder="请输入用户名" v-model="user.username" :disabled="user.id!=null"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机号">
            <el-input placeholder="请输入手机号" v-model="user.phone"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="邮箱">
            <el-input placeholder="请输入邮箱" v-model="user.email"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="年龄">
            <el-input placeholder="请输入年龄" v-model="user.age"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="6">
          <el-form-item label="职别">
            <el-select placeholder="请选择" v-model="user.level" @change="levelSelect">
              <el-option v-for="item in levelOptions" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="直属领导">
            <el-select placeholder="请选择" v-model="user.parentId">
              <el-option v-for="item in leaderOptions" :label="item.username" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="性别">
            <el-radio-group v-model="user.gender">
              <el-radio border label="男" value="1" style="margin: 0"></el-radio>
              <el-radio border label="女" value="0"></el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="用户状态">
            <el-radio-group v-model="user.status">
              <el-radio border label="启用" value="1" style="margin: 0"></el-radio>
              <el-radio border label="禁用" value="0"></el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="btn">保存</el-button>
    </template>
  </el-dialog>
  <div style="height: 94vh">
    <el-card style="margin: 10px; height: 70px">
      <el-form :inline="true">
        <el-form-item label="用户名">
          <el-input placeholder="请输入用户名" v-model="user1.username" style="width: 220px"
                    @keydown.enter="select"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select placeholder="请选择" v-model="user1.status" style="width: 220px" @change="select">
            <el-option label="启用" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="select">查询</el-button>
          <el-button @click="resist1">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card style="margin: 10px">
      <el-table :data="userArr" style="width: 100%">
        <el-table-column type="index" label="编号" width="80px" align="center"></el-table-column>
        <el-table-column prop="username" label="用户名" align="center"></el-table-column>
        <el-table-column prop="phone" label="手机号" align="center"></el-table-column>
        <el-table-column prop="createTime" label="加入时间" align="center"></el-table-column>
        <el-table-column label="用户状态" align="center">
          <template #default="scope">
            <el-switch v-model="scope.row.status" active-value="1" inactive-value="0" active-color="#13ce66"
                       inactive-color="#ff4949"
                       :disabled="scope.row.level==40"
                       @change="updateStatus(scope.row.id, scope.row.status)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="scope">
                <el-button link type="primary" @click="resist(scope.row.id)" :disabled="scope.row.level==40">
                  密码重置
                </el-button>
                <el-button link type="primary" @click="update(scope.row.id)" :disabled="scope.row.level==40">
                  编辑
                </el-button>
                <el-button link type="primary" @click="del(scope.$index, scope.row)" :disabled="scope.row.level==40">
                  删除
                </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<style scoped>

</style>